package plenprojects.security;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

public class SecurityToken {
	
	private final String username;
	private final String tokenValue;
	private final Set<Permission> permissions = new HashSet<Permission>();

	public SecurityToken(String tokenValue, String username) {
		
		this.tokenValue = tokenValue;
		this.username = username;
	}
	
	public void addPermission(Permission p) {
		
		permissions.add(p);
	}
	
	public void removePermission(String chatroom, PermissionType type) {
		
		Iterator<Permission> iter = permissions.iterator();
		
		while(iter.hasNext()) {
			
			Permission perm = iter.next();
			
			if(perm.getChatroom().equals(chatroom) && perm.getType().equals(type)) {
				
				iter.remove();
			}
		}
	}
	
	public List<Permission> getPermissions() {
		
		return new ArrayList<Permission>(permissions);
	}

	public String getUsername() {
		
		return username;
	}

	@Override
	public String toString() {
		
		return tokenValue;
	}
	
}
